package bb.lanxing.databinding.sprint;

import android.text.format.Formatter;
import androidx.core.content.ContextCompat;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.lib.devices.core.sync.DeviceFile;
import bb.lanxing.lib.devices.core.sync.DeviceFileStatus;
import bb.lanxing.lib.devices.core.sync.FitDeviceFile;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.DateUtil;
import bb.lanxing.util.AppFileUtils;

public class DeviceSyncItem extends BaseObservable {
    private DeviceFile deviceFile;
    private float progress;
    private int status;

    public DeviceSyncItem(DeviceFile deviceFile) {
        setDeviceFile(deviceFile);
    }

    public DeviceSyncItem() {
    }

    public DeviceFile getDeviceFile() {
        return this.deviceFile;
    }

    public void setDeviceFile(DeviceFile deviceFile) {
        this.deviceFile = deviceFile;
        notifyChange();
    }

    @Bindable
    public String getTitle() {
        DeviceFile deviceFile = this.deviceFile;
        if (!(deviceFile instanceof FitDeviceFile)) {
            return null;
        }
        FitDeviceFile fitDeviceFile = (FitDeviceFile) deviceFile;
        return fitDeviceFile.getStartTime() > 0 ? DateUtil.format(fitDeviceFile.getStartTime()) : fitDeviceFile.getName();
    }

    @Bindable
    public String getSize() {
        DeviceFile deviceFile = this.deviceFile;
        if (!(deviceFile instanceof FitDeviceFile)) {
            return null;
        }
        FitDeviceFile fitDeviceFile = (FitDeviceFile) deviceFile;
        if (fitDeviceFile.getDistance() > 0) {
            return CommonUtil.getKMDistance(fitDeviceFile.getDistance() / 1000.0d) + "Km";
        } else if (fitDeviceFile.getSize() > 0) {
            return Formatter.formatFileSize(App.getContext(), fitDeviceFile.getSize());
        } else {
            if (fitDeviceFile.getDuration() <= 0) {
                return null;
            }
            return DateUtil.format2.format(fitDeviceFile.getDuration());
        }
    }

    @Bindable
    public boolean getShowProgressBar() {
        return getStatus() == 1;
    }

    @Bindable
    public int getProgressInt() {
        return (int) this.progress;
    }

    @Bindable
    public float getProgress() {
        return this.progress;
    }

    public void setProgress(float f) {
        this.progress = f;
        notifyPropertyChanged(144);
        notifyPropertyChanged(145);
    }

    @Bindable
    public String getStatusText() {
        Integer valueOf;
        int i = this.status;
        if (i == 1) {
            valueOf = (int) R.string.str_sync_import_data;
        } else if (i == 2) {
            valueOf = (int) R.string.str_sync_import_data;
        } else if (i == 3) {
            valueOf = (int) R.string.str_sync_already_exists;
        } else if (i == 4) {
            valueOf = (int) R.string.str_sync_failed;
        } else if (i == 5) {
            valueOf = (int) R.string.str_sync_pending;
        } else {
            valueOf = (int) R.string.str_sync_not_exists;
            if (AppFileUtils.isFileExists(this.deviceFile.getPath())) {
                valueOf = (int) R.string.str_sync_already_exists;
            }
        }
        return App.getContext().getString(valueOf);
    }

    @Bindable
    public Integer getStatusTextColor() {
        if (AppFileUtils.isFileExists(this.deviceFile.getPath())) {
            return ContextCompat.getColor(App.getContext(), R.color.common_blue);
        }
        int i = this.status;
        if (i == 1 || i == 2 || i == 3 || i == 5) {
            return ContextCompat.getColor(App.getContext(), R.color.common_blue);
        }
        return ContextCompat.getColor(App.getContext(), R.color.common_red);
    }

    @Bindable
    public int getStatusImage() {
        int vDrawableId = -1;
        DeviceFileStatus vStatus = DeviceFileStatus.valueOf(getStatus());
        if (vStatus == null) {
            vStatus = DeviceFileStatus.STATUS_NONE;
        }
        switch (vStatus) {
            case STATUS_SYNCED -> vDrawableId = R.drawable.ic_sync_success;
            case STATUS_SYNCING -> vDrawableId = R.drawable.ic_sync_ing;
            case STATUS_SYNC_FAIL -> vDrawableId = R.drawable.ic_sync_error;
            case STATUS_SYNC_PENDING -> vDrawableId = R.drawable.shape_transparent;
        }
        return vDrawableId;
//        return i != 1 ? i != 2 ? i != 3 ? R.drawable.shape_transparent : R.drawable.ic_sync_ing : R.drawable.ic_sync_error : R.drawable.ic_sync_success;
    }

    @Bindable
    public int getStatus() {
        return this.status;
    }

    public void setStatus(Integer num) {
        this.status = num;
        notifyPropertyChanged(192);
        notifyPropertyChanged(194);
        notifyPropertyChanged(195);
        notifyPropertyChanged(181);
        notifyPropertyChanged(193);
    }

    public Long getFileId() {
        DeviceFile deviceFile = this.deviceFile;
        return deviceFile != null ? deviceFile.getId() : -1L;
    }

    public void reset() {
        this.status = DeviceFileStatus.STATUS_NONE.getStatus();
        this.deviceFile = null;
        this.progress = 0.0f;
    }
}
